{extend name="../../../app/backend/view/common/layout" /}

{block name="body"}
<body>
<div class="container">
    <!-- 搜索栏 -->
    <form class="layui-form layui-search">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label for="table_name" class="layui-form-label">表名称：</label>
                <div class="layui-input-inline">
                    <input type="text" id="table_name" name="table_name" class="layui-input" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <label for="table_comment" class="layui-form-label">表描述：</label>
                <div class="layui-input-inline">
                    <input type="text" id="table_comment" name="table_comment" class="layui-input" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-sm layui-btn-default" lay-submit lay-filter="search">查询</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" lay-submit lay-filter="clear-search">重置</a>
            </div>
        </div>
    </form>

    <!-- 表格栏 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="wait-table-list" lay-filter="wait-table-list"></table>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <a class="layui-btn layui-btn-sm layui-btn-default {:check_perms('import')}" lay-event="import">
                        <i class="layui-icon icon-add"></i>
                        <span>导入表结构</span>
                    </a>
                    <a class="layui-btn layui-btn-sm layui-btn-danger layui-btn-forbid {:check_perms('del')}" lay-event="leave">
                        <i class="layui-icon icon-del"></i>
                        <span>删除</span>
                    </a>
                </div>
            </script>
            <script type="text/html" id="table-operate">
                <button type="button" class="layui-btn layui-btn-xs layui-btn-primary layui-border-green {:check_perms('preview')}" lay-event="preview" lay-tips="预览">
                    <i class="layui-icon icon-eye-fill"></i>
                </button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-default {:check_perms('update')}" lay-event="update" lay-tips="编辑">
                    <i class="layui-icon icon-edit"></i>
                </button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-warm {:check_perms('synchrony')}" lay-event="synchrony" lay-tips="同步">
                    <i class="layui-icon icon-seckill-flash"></i>
                </button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-gloomy {:check_perms('exports')}" lay-event="exports" lay-tips="生成">
                    <i class="layui-icon icon-download-arrow"></i>
                </button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-danger {:check_perms('destroy')}" lay-event="destroy" lay-tips="销毁">
                    <i class="layui-icon icon-del"></i>
                </button>
            </script>
        </div>
    </div>
</div>
</body>
{/block}

{block name="js"}
<script>
    layui.use(function() {
        // 渲染表格
        let table = waitUtil.table({
            elem: '#wait-table-list'
            ,url: '{:route("addons/curd/gen/index")}'
            ,cols: [[
                {type:'checkbox', width:48},
                {field:'table_name', title:'表名称', minWidth:150},
                {field:'table_comment', title:'表描述', minWidth:150},
                {field:'table_engine', title:'表引擎', minWidth:80},
                {field:'author', title:'作者', minWidth:110},
                {field:'tpl_type', title:'模板类型', minWidth:85, align:'center'},
                {field:'gen_type', title:'生成方式', minWidth:85, align:'center'},
                {field:'gen_module', title:'生成模块', minWidth:85, align:'center'},
                {field:'menu_type', title:'菜单构建', minWidth:85, align:'center'},
                {field:'join_status', title:'子表关联', minWidth:85, align:'center'},
                {field:'create_time', title:'创建时间', minWidth:165, align:'center'},
                {field:'update_time', title:'更新时间', minWidth:165, align:'center'},
                {fixed:'right', title:'操作', width:170, align:'center', toolbar:'#table-operate'}
            ]]
        });

        // 逻辑事件
        waitUtil.event({
            import: function () {
                waitUtil.popup({
                    title: '选择生成表',
                    url: '{:route("addons/curd/gen/tables")}',
                    area: ['750px', '600px'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function() {
                            let tableNames = waitUtil.checkbox({layero: layero, key: 'name'});
                            if (tableNames.length <= 0) {
                                return layer.msg('请至少选择一项！', {icon: 2});
                            }
                            waitUtil.locking(this);
                            waitUtil.ajax({
                                url: '{:route("addons/curd/gen/imports")}',
                                type: 'POST',
                                data: {tableNames: tableNames}
                            }).then((res) => {
                                waitUtil.unlock(this);
                                if (res.code === 0) {
                                    table.reload({page: {curr: 1}});
                                    layer.close(index);
                                }
                            }).catch(() => {
                                waitUtil.unlock(this);
                            });
                        });
                    }
                });
            },
            update: function (obj) {
                waitUtil.popup({
                    title: '更新表结构',
                    url: '{:route("addons/curd/gen/update")}?id='+obj.data.id,
                    area: ['95%', '95%'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function(data) {
                            waitUtil.locking(this);
                            data.field['id'] = obj.data.id;
                            waitUtil.ajax({
                                url: '{:route("addons/curd/gen/update")}',
                                type: 'POST',
                                data: data.field
                            }).then((res) => {
                                waitUtil.unlock(this);
                                if (res.code === 0) {
                                    table.reload();
                                    layer.close(index);
                                }
                            }).catch(() => {
                                waitUtil.unlock(this);
                            });
                            return true;
                        });
                    }
                });
            },
            destroy: function (obj) {
                layer.confirm('确定要删除此项数据吗？', function(index) {
                    layer.close(index);
                    waitUtil.ajax({
                        url: '{:route("addons/curd/gen/destroy")}',
                        type: 'POST',
                        data: {ids: [obj.data.id]}
                    }).then((res) => {
                        if (res.code === 0) {
                            table.reload();
                        }
                    });
                });
            },
            preview: function (obj) {
                waitUtil.popup({
                    title: '预览代码',
                    url: '{:route("addons/curd/gen/preview")}?id='+obj.data.id,
                    area: ['95%', '95%'],
                    success: function (layero, index) {}
                });
            },
            exports: function (obj) {
                layer.confirm('确定要'+obj.data.gen_type+'吗？', function(index) {
                    layer.close(index);
                    if (obj.data.gen_type==='下载') {
                        if (obj.data.menu_type === '自动') {
                            layer.confirm('是否同时创建菜单?', function (i) {
                                layer.close(i);
                                waitUtil.ajax({
                                    url: '{:route("addons/curd/gen/initMenu")}',
                                    type: 'POST',
                                    data: {id: obj.data.id},
                                });
                                window.open('{:route("addons/curd/gen/download")}?id=' + obj.data.id, '_blank');
                            }, function () {
                                window.open('{:route("addons/curd/gen/download")}?id=' + obj.data.id, '_blank');
                            });
                        } else {
                            window.open('{:route("addons/curd/gen/download")}?id=' + obj.data.id, '_blank');
                        }
                    } else {
                        waitUtil.ajax({
                            url: '{:route("addons/curd/gen/exports")}',
                            type: 'POST',
                            data: {id: obj.data.id}
                        });
                    }
                });
            },
            synchrony: function (obj) {
                layer.confirm(`确定要同步表结构吗？`, function(index) {
                    layer.close(index);
                    waitUtil.ajax({
                        url: '{:route("addons/curd/gen/synchrony")}',
                        type: 'POST',
                        data: {id: obj.data.id}
                    });
                });
            },
            leave: function () {
                let ids = waitUtil.checkbox();
                if (!ids.length) {
                    return layer.msg('请至少选择一项！', {icon: 2});
                }
                layer.confirm(`确定要删除选中的${ids.length}项数据吗？`, function(index) {
                    layer.close(index);
                    waitUtil.ajax({
                        url: '{:route("auth.admin/del")}',
                        type: 'POST',
                        data: {ids: ids}
                    }).then((res) => {
                        if (res.code === 0) {
                            table.reload();
                        }
                    });
                });
            }
        });

        // 搜索事件
        waitUtil.search(table);
    });
</script>
{/block}